package com.visikard.system;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.visikard.item.VKregisterMerchantSession;

/**
 * Visikard session item, leaning from com.facebook.Session
 * @see com.facebook.Session
 * @author TuanNgo
 *
 */
public class VKMerchantSessionStore {
	private static final String TAG = VKMerchantSessionStore.class.getSimpleName();

	/* session app key */
	private static final String SSKEY = "visikar_register_merchant_preference_name";
	private static final String SSKEY_LOCATION = "visikar_register_preference_address";

	/* session sub keys */
	private static final String SSKEY_USERNAME = "username";
	private static final String SSKEY_PASSWORD = "password";
	private static final String SSKEY_SESSION_API = "loginSession";
	private static final String SSKEY_SESSION_INIT = "initSession";

	private static final String SSKEY_USER_ID = "uid";
	private static final String SSKEY_KARD_ID = "idKard";
	private static final String SSKEY_CITY_NAME = "nameCity";

	private static final String SSKEY_ADDRESS = "Address";
	private static final String SSKEY_MERCHANT_LOGO = "MerchantLogo";
	private static final String SSKEY_MERCHANT_NAME = "MerchantName";

	/**
	 * Save the access token and expiry date so you don't have to fetch it each
	 * time
	 */
	public static void save(VKregisterMerchantSession session, Context context) {
		Log.d(TAG, "Saving session info");
		if (session == null) {
			Log.d(TAG, "Saving session info. provided session is null");
			return;
		}

		Editor editor = context.getSharedPreferences(SSKEY, Context.MODE_PRIVATE).edit();
		editor.putString(SSKEY_USERNAME, session.getUsername());
		editor.putString(SSKEY_PASSWORD, session.getPassword());
		editor.putString(SSKEY_SESSION_API, session.getLoginSession());
		editor.putString(SSKEY_SESSION_INIT, session.getInitSession());
		editor.putLong(SSKEY_USER_ID, session.getUserId());
		editor.putLong(SSKEY_KARD_ID, session.getKardId());
		editor.putString(SSKEY_CITY_NAME, session.getCityName());
		editor.putString(SSKEY_MERCHANT_LOGO, session.getMerchantLogo());
		editor.putString(SSKEY_MERCHANT_NAME, session.getMerchantName());

		// return editor.commit();
		boolean saved = editor.commit();
		Log.d(TAG, "Saving session info. Success?: " + saved);
	}

	/**
	 * Restore the access token and the expiry date from the shared preferences.
	 */
	public static VKregisterMerchantSession restore(Context context) {
		Log.d(TAG, "Get session info");

		SharedPreferences savedSession = context.getSharedPreferences(SSKEY, Context.MODE_PRIVATE);
		if (savedSession.getAll().size() == 0)
			return null;
		VKregisterMerchantSession session = new VKregisterMerchantSession();
		session.setUsername(savedSession.getString(SSKEY_USERNAME, session.getUsername()));
		session.setPassword(savedSession.getString(SSKEY_PASSWORD, session.getPassword()));
		session.setLoginSession(savedSession.getString(SSKEY_SESSION_API, session.getLoginSession()));
		session.setInitSession(savedSession.getString(SSKEY_SESSION_INIT, session.getInitSession()));
		session.setUserId(savedSession.getLong(SSKEY_USER_ID, session.getUserId()));
		session.setKardId(savedSession.getLong(SSKEY_KARD_ID, session.getKardId()));
		session.setCityName(savedSession.getString(SSKEY_CITY_NAME, session.getCityName()));
		session.setMerchantLogo(savedSession.getString(SSKEY_MERCHANT_LOGO, ""));
		session.setMerchantName(savedSession.getString(SSKEY_MERCHANT_NAME, ""));

		return session;
	}

	/**
	 * Save the access token and expiry date so you don't have to fetch it each
	 * time
	 */
	public static void saveAddress(String session, Context context) {
		if (session == null || session.equals(""))
			return;
		Editor editor = context.getSharedPreferences(SSKEY_LOCATION, Context.MODE_PRIVATE).edit();
		editor.putString(SSKEY_ADDRESS, session);
		// return editor.commit();
		boolean saved = editor.commit();
		Log.d("NghiDo", "Saving address. Success?: " + saved);
	}

	public static String restoreAdress(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(SSKEY_LOCATION, Context.MODE_PRIVATE);
		return savedSession.getString(SSKEY_ADDRESS, "");
	}

	/**
	 * clear session
	 * @param context
	 */
	public static void clear(Context context) {
		Editor editor = context.getSharedPreferences(SSKEY, Context.MODE_PRIVATE).edit();
		editor.clear();
		editor.commit();
	}
}